import javax.swing.*;
import javax.swing.plaf.metal.MetalLookAndFeel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 * Creates a GUI for a Red Black Tree
 * @author kevin curtin, Ryan Diaz
 *
 */
public class RedBlackGUI extends JFrame {
	static RedBlackTree<Integer> tree;
	static JButton addButton, removeButton, findButton, clearButton;
	static JTextField addField, removeField, findField;
	static JPanel buttonPanel, treePanel;
	static JLabel printLabel, emptyLabel, findLabel;
	static TreePrinter printer;
	public RedBlackGUI(){
		setLayout(new GridLayout(2, 1));
		tree = new RedBlackTree<Integer>();
		
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout());
		add(buttonPanel);
		
		printer = new TreePrinter();
		printLabel = new JLabel();
		addButton = new JButton("Add");
		addButton.addActionListener(new action());
		addField = new JTextField();
		addField.setColumns(5);
		removeButton = new JButton("Remove");
		removeButton.addActionListener(new action());
		removeField = new JTextField();	
		removeField.setColumns(5);
		findButton = new JButton("Find");
		findButton.addActionListener(new action());
		findField = new JTextField();
		findField.setColumns(5);
		clearButton = new JButton("Clear");
		clearButton.addActionListener(new action());
		
		buttonPanel.add(addButton);
		buttonPanel.add(addField);
		buttonPanel.add(removeButton);
		buttonPanel.add(removeField);
		buttonPanel.add(findButton);
		buttonPanel.add(findField);
		buttonPanel.add(clearButton);
		findLabel = new JLabel();
		
		treePanel = new JPanel();
		treePanel.setLayout(new BorderLayout());
		treePanel.add(findLabel, BorderLayout.NORTH);
		emptyLabel = new JLabel("                                                                          ");
		treePanel.add(emptyLabel, BorderLayout.WEST);
		treePanel.add(printLabel, BorderLayout.CENTER);
		add(treePanel);
		setTitle("Red Black Tree");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		setSize(800,800);
		setLocationRelativeTo(null);
		
		
	}
	static void printTree() {
		//System.out.println(printer.printNode(tree.root));
		printLabel.setText("<HTML><font face='Consolas'<pre>" + printer.printNode(tree.root) +"</pre></font></HTML>");
	}
	class action implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent ae) {
			// TODO Auto-generated method stub
			if(ae.getActionCommand().equals("Add")){
				try {
					RedBlackGUI.tree.add(Integer.parseInt(RedBlackGUI.addField.getText()));
					printTree();
					addField.setText("");
				}
				catch(NumberFormatException nfe) {
					System.out.println("Invalid Input");
					addField.setText("");
				}
				
			}
			else if(ae.getActionCommand().equals("Remove")){
				try {
					RedBlackGUI.tree.remove(Integer.parseInt(RedBlackGUI.removeField.getText()));
					printTree();
					removeField.setText("");
				}
				catch(NumberFormatException nfe) {
					System.out.println("Invalid Input");
					removeField.setText("");
				}
			}
			else if(ae.getActionCommand().equals("Find")){
				try {
					int temp = Integer.parseInt(RedBlackGUI.findField.getText());
					if(RedBlackGUI.tree.find(temp)){
						findLabel.setText("            " + temp + " found!");
					}
					else {
						findLabel.setText("            " + temp + " not found.");
					}
					findField.setText("");
				}
				catch(NumberFormatException nfe) {
					System.out.println("Invalid Input");
					findField.setText("");
				}
			}
			else if(ae.getActionCommand().equals("Clear")) {
				RedBlackGUI.tree.clear();
				findLabel.setText("");
				printTree();
			}
		}
		
	}
}

